package com.xing.other;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.LockSupport;

/**
 * Author: linjx
 * Date: 2018/12/17
 */
public class OtherTest {


    public static void main(String[] args) throws Exception{
        ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 10, 10,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(100),
                new ThreadFactory() {
                    private AtomicInteger max = new AtomicInteger(0);
                    @Override
                    public Thread newThread(Runnable r) {
                        return new Thread(r, "coupon-push-" + max.incrementAndGet());
                    }
                });
        for (int i = 0 ; i < 10; i++) {
            int finalI = i;
            executor.execute(() -> {
                System.out.println(finalI);
                LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(2));
            });
        }
        executor.allowCoreThreadTimeOut(true);
        System.in.read();
    }
}
